﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JYH.Data;
using Newtonsoft.Json;
using JYH.Model;
using JYH.DataAccess;
using JYH.Framework;
using System.Security.Cryptography;
using System.Runtime.CompilerServices;
using JYH.Model.API;
using System.Net;

namespace JYH.Business
{

    /// <summary>
    ///  BasMaterialInfo
    /// </summary> 
    public class BasMaterialInfoBLL : BusinessBase<BasMaterialInfo>
    {


        /// <summary>
        ///  BasMaterialInfo
        /// </summary> 
        public BasMaterialInfoBLL()
        {
            myDataBase = DB.D();
            this.SqlSelect = @"; Select BasMaterialInfo.* From BasMaterialInfo";
        }

       /// <summary>
       /// 第三方系统同步到MES系统
       /// </summary>
       /// <param name="ErpLIist">第三方物料信息集合</param>
       /// <param name="Msg"></param>
       /// <returns></returns>
        public static bool SyncMaterialInfo(List<BasMaterialInfo> ErpLIist, out string Msg)
        {

            Msg = string.Empty;
            Database db= DB.D();
            db.BeginTransaction();
            Sql sql = new Sql();
            StringBuilder sb = new StringBuilder();

            List<BasMaterialInfo> MesM = new List<BasMaterialInfo>();
            try
            {
               
                if (ErpLIist.Count > 0)
                {
                    sql = new Sql();
                    sql.Append("Select * From BasWarehouseInfo");
                    sql.OrderBy("BasWarehouseInfo.WarehouseNO");
                    List<BasWarehouseInfo> Warehouses = new List<BasWarehouseInfo>();
                    Warehouses=db.Fetch<BasWarehouseInfo>(sql);


                    sql = new Sql();
                    sql.Append("Select * From BasUnitInfo");
                    sql.OrderBy("BasUnitInfo.UnitNO");
                    List<BasUnitInfo> Units = new List<BasUnitInfo>();
                    Units = db.Fetch<BasUnitInfo>(sql);


                    string NOs = "'',";
                    foreach (BasMaterialInfo m in ErpLIist)
                    {
                        NOs += "'" + StringHelper.ConvertFullWidthToHalfWidth(m.MaterialNO)+ "',";
                    }
                    NOs = NOs.Substring(0, NOs.Length - 1);
                    sql = new Sql();
                    sql.Append("Select BasMaterialInfo.* From BasMaterialInfo");
                    sql.Where("BasMaterialInfo.MaterialNO in (" + NOs + ")");
                    sql.OrderBy("BasMaterialInfo.MaterialNO");
                    MesM = db.Fetch<BasMaterialInfo>(sql.SQL);
                    foreach (BasMaterialInfo m in ErpLIist)
                    {
                        int index = MesM.FindIndex(p => StringHelper.ConvertFullWidthToHalfWidth( p.MaterialNO) == StringHelper.ConvertFullWidthToHalfWidth(m.MaterialNO));
                        if (index > -1)
                        {

                            int row = -1;
                            row = Warehouses.FindIndex(p => p.WarehouseNO == m.WarehouseNO);
                            if (row > -1)
                            {
                                MesM[row].WarehouseID = Warehouses[row].WarehouseID;
                            }
                            row = -1;
                            row = Units.FindIndex(p => p.UnitNO == m.UnitNO);
                            if (row > -1)
                            {
                                MesM[index].UnitID = Units[row].UnitID;
                            }
                            MesM[index].MaterialNO = StringHelper.ConvertFullWidthToHalfWidth(m.MaterialNO);
                            MesM[index].MaterialName = StringHelper.ConvertFullWidthToHalfWidth( m.MaterialName);
                            MesM[index].MaterialDesc = StringHelper.ConvertFullWidthToHalfWidth(m.MaterialDesc);
                            sb.Append(BusinessTool.GetUpdateSql(MesM[index]));
                        }
                        else
                        {
                            BasMaterialInfo obj = new BasMaterialInfo();

                            int row = -1;
                            row = Warehouses.FindIndex(p => p.WarehouseNO == m.WarehouseNO);
                            if (row > -1)
                            {
                                obj.WarehouseID = Warehouses[row].WarehouseID;
                            }
                            row = -1;
                            row = Units.FindIndex(p => p.UnitNO == m.UnitNO);
                            if (row > -1)
                            {
                                obj.UnitID = Units[row].UnitID;
                            }
                            obj.MaterialNO = StringHelper.ConvertFullWidthToHalfWidth(m.MaterialNO);
                            obj.MaterialDesc = StringHelper.ConvertFullWidthToHalfWidth(m.MaterialDesc);
                            obj.MaterialName = StringHelper.ConvertFullWidthToHalfWidth(m.MaterialName);
                            obj.MaterialID = Guid.NewGuid().ToString();
                            obj.RState ="1";
                            obj.IsExceed = 1;
                            obj.Exceeding = 0.0m;
                            obj.StockQty = 0;
                            MesM.Add(obj);
                            sb.Append(BusinessTool.GetInsertSql(obj));
                        }
                    }
                }
                if (sb.Length > 0)
                {
                    db.Execute(sb.ToString());
                }
                db.CompleteTransaction();
                return true;
            }
            catch (Exception e)
            {
                Msg = e.Message;
                db.AbortTransaction();
                return false;
            }


        }

        
    }

}
